package com.lzh.shijicloudstack.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzh.shijicloudstack.mapper.CookbookCategoryMapper;
import com.lzh.shijicloudstack.model.dto.cookbookCategory.AddCookbookCategoryDetailDto;
import com.lzh.shijicloudstack.model.dto.cookbookCategory.EditCookbookCategoryDetailDto;
import com.lzh.shijicloudstack.model.entity.CookbookCategory;
import com.lzh.shijicloudstack.model.vo.cookbookCategory.CookbookCategoryDetailVO;
import com.lzh.shijicloudstack.service.CookbookCategoryService;
import com.lzh.shijicloudstack.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 菜谱分类服务实现类
 *
 * @author luzhiheng
 * @date 2025-02-17
 */
@Service
public class CookbookCategoryServiceImpl extends ServiceImpl<CookbookCategoryMapper, CookbookCategory> implements CookbookCategoryService {

    @Override
    public List<CookbookCategoryDetailVO> findList() {
        List<CookbookCategory> cookbookCategoryList = baseMapper.selectList(null);
        List<CookbookCategory> parentList = cookbookCategoryList.stream().filter(e -> e.getParentId() == -1).collect(Collectors.toList());
        List<CookbookCategory> childList = cookbookCategoryList.stream().filter(e -> e.getParentId() != -1).collect(Collectors.toList());
        Map<Long, List<CookbookCategory>> parentIdChildList = childList.stream().collect(Collectors.groupingBy(CookbookCategory::getParentId));

        return parentList.stream().map(e -> {
            CookbookCategoryDetailVO vo = new CookbookCategoryDetailVO();
            vo.setId(e.getId());
            vo.setValue(e.getId());
            vo.setName(e.getName());
            vo.setLabel(e.getName());
            vo.setParentId(e.getParentId());

            List<CookbookCategory> categoryList = parentIdChildList.get(e.getId()) != null ? parentIdChildList.get(e.getId()) : new ArrayList<>();
            List<CookbookCategoryDetailVO> children = categoryList.stream().map(k -> {
                CookbookCategoryDetailVO detailVO = new CookbookCategoryDetailVO();
                detailVO.setId(k.getId());
                detailVO.setValue(k.getId());
                detailVO.setName(k.getName());
                detailVO.setLabel(k.getName());
                detailVO.setParentId(k.getParentId());
                return detailVO;
            }).collect(Collectors.toList());
            vo.setChildren(children);
            return vo;
        }).collect(Collectors.toList());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean edit(EditCookbookCategoryDetailDto dto) {
        CookbookCategory cookbookCategory = BeanCopyUtils.copyBean(dto, CookbookCategory.class);
        baseMapper.updateById(cookbookCategory);

        baseMapper.deleteByParentId(dto.getId());

        List<EditCookbookCategoryDetailDto> children = dto.getChildren();
        List<CookbookCategory> cookbookCategoryList = children.stream().map(e -> {
            CookbookCategory category = new CookbookCategory();
            category.setName(e.getName());
            category.setParentId(dto.getId());
            return category;
        }).collect(Collectors.toList());
        this.saveBatch(cookbookCategoryList);

        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean add(AddCookbookCategoryDetailDto dto) {
        CookbookCategory cookbookCategory = BeanCopyUtils.copyBean(dto, CookbookCategory.class);
        cookbookCategory.setParentId(-1L);
        baseMapper.insert(cookbookCategory);

        List<AddCookbookCategoryDetailDto> children = dto.getChildren();
        List<CookbookCategory> cookbookCategoryList = children.stream().map(e -> {
            CookbookCategory category = new CookbookCategory();
            category.setName(e.getName());
            category.setParentId(cookbookCategory.getId());
            return category;
        }).collect(Collectors.toList());
        this.saveBatch(cookbookCategoryList);

        return true;
    }

}
